package webExperiment;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * Participant Panel (V 2.2) (c) O.Lindemann
 * 
 */

public class ParticipantPanel extends javax.swing.JPanel {
    private static final long serialVersionUID = 1L;

    public static boolean askInitials = true; // change before constructor
    public static boolean askYearOfBirth = true;
    public static boolean askMTongue = true;
    public static boolean askCountryOfBirth = true;
    public static boolean askCityOfBirth = true;
    public static boolean askResidence = true;
    public static boolean askGender = true;
    public static boolean askHandedness = false;
    public static boolean allIsObligatory = false;

    public static class Labels {
	public String initials;
	public String yearOfBirth;
	public String countryOfBirth;
	public String cityOfBirth;
	public String mtongue;
	public String gender;
	public String female;
	public String male;
	public String residence;
	public String handedness;
	public String left;
	public String right;
    }

    private JLabel txtInitials = new JLabel();
    private JLabel txtYoB = new JLabel();
    private JLabel txtMTong = new JLabel();
    private JLabel txtConB = new JLabel();
    private JLabel txtCityB = new JLabel();
    private JLabel txtRes = new JLabel();
    private JLabel txtGender = new JLabel();
    private JLabel txtHandedness = new JLabel();
    private JTextField inInitials = new JTextField();
    private JTextField inYoB = new JTextField();
    private JTextField inMTong = new JTextField();
    private JTextField inConB = new JTextField();
    private JTextField inCityB = new JTextField();
    private JTextField inRes = new JTextField();
    private JComboBox cmBoxGender = new JComboBox();
    private JComboBox cmBoxHandedness = new JComboBox();
    public JButton okButton = new JButton();

    public ParticipantPanel(String okBtnText) {
	this.setVisible(false);
	this.setSize(400, 400);
	this.setLocation(50, 50);
	// this.setBackground(Color.white);
	this.setLayout(new FlowLayout(FlowLayout.CENTER));
	Dimension d = new Dimension(100, 30);
	txtInitials.setPreferredSize(d);
	txtYoB.setPreferredSize(d);
	txtMTong.setPreferredSize(d);
	txtConB.setPreferredSize(d);
	txtCityB.setPreferredSize(d);
	txtRes.setPreferredSize(d);
	txtGender.setPreferredSize(d);
	txtHandedness.setPreferredSize(d);
	d = new Dimension(200, 30);
	inInitials.setPreferredSize(d);
	inYoB.setPreferredSize(d);
	inMTong.setPreferredSize(d);
	inConB.setPreferredSize(d);
	inCityB.setPreferredSize(d);
	inRes.setPreferredSize(d);
	cmBoxGender.setPreferredSize(d);
	cmBoxHandedness.setPreferredSize(d);

	if (askInitials) {
	    add(txtInitials);
	    add(inInitials);
	}
	if (askGender) {
	    add(txtGender);
	    add(cmBoxGender);
	}
	if (askYearOfBirth) {
	    add(txtYoB);
	    add(inYoB);
	}
	if (askHandedness) {
	    add(txtHandedness);
	    add(cmBoxHandedness);
	}
	if (askMTongue) {
	    add(txtMTong);
	    add(inMTong);
	}
	if (askCountryOfBirth) {
	    add(txtConB);
	    add(inConB);
	}
	if (askCityOfBirth) {
	    add(txtCityB);
	    add(inCityB);
	}
	if (askResidence) {
	    add(txtRes);
	    add(inRes);
	}

	okButton.setText(okBtnText);
	okButton.setPreferredSize(d);
	add(okButton);
    }

    public void writeLabels(ParticipantPanel.Labels lb) {
	txtInitials.setText(lb.initials);
	txtYoB.setText(lb.yearOfBirth);
	txtMTong.setText(lb.mtongue);
	txtConB.setText(lb.countryOfBirth);
	txtCityB.setText(lb.cityOfBirth);
	txtRes.setText(lb.residence);
	cmBoxGender.removeAllItems();
	cmBoxGender.insertItemAt("?", 0);
	cmBoxGender.insertItemAt(lb.female, 1);
	cmBoxGender.insertItemAt(lb.male, 2);
	cmBoxGender.setSelectedIndex(0);
	txtGender.setText(lb.gender);

	cmBoxHandedness.removeAllItems();
	cmBoxHandedness.insertItemAt("?", 0);
	cmBoxHandedness.insertItemAt(lb.left, 1);
	cmBoxHandedness.insertItemAt(lb.right, 2);
	cmBoxHandedness.setSelectedIndex(0);
	txtHandedness.setText(lb.handedness);

    }

    public void setCenterLocation(Dimension refPanelSize) {
	setLocation((refPanelSize.width - this.getWidth()) / 2, (int) Math
		.round((refPanelSize.height - this.getHeight()) / 2));
    }

    public boolean isObligatoryFilledIn() {
	boolean missing = false;
	txtGender.setForeground(Color.BLACK);
	txtInitials.setForeground(Color.BLACK);
	txtYoB.setForeground(Color.BLACK);
	txtMTong.setForeground(Color.BLACK);
	txtConB.setForeground(Color.BLACK);
	txtCityB.setForeground(Color.BLACK);
	txtRes.setForeground(Color.BLACK);

	if (cmBoxGender.getSelectedIndex() <= 0 && askGender) {
	    txtGender.setForeground(Color.red);
	    missing = true;
	}
	if (inYoB.getText().length() < 2 && askYearOfBirth) {
	    txtYoB.setForeground(Color.red);
	    missing = true;
	}

	if (allIsObligatory) {
	    if (inInitials.getText().length() < 2 && askInitials) {
		txtInitials.setForeground(Color.red);
		missing = true;
	    }
	    if (cmBoxHandedness.getSelectedIndex() <= 0 && askHandedness) {
		txtHandedness.setForeground(Color.red);
		missing = true;
	    }
	    if (inMTong.getText().length() < 2 && askMTongue) {
		txtMTong.setForeground(Color.red);
		missing = true;
	    }
	    if (inConB.getText().length() < 2 && askCountryOfBirth) {
		txtConB.setForeground(Color.red);
		missing = true;
	    }
	    if (inCityB.getText().length() < 2 && askCityOfBirth) {
		txtCityB.setForeground(Color.red);
		missing = true;
	    }
	    if (inRes.getText().length() < 2 && askResidence) {
		txtRes.setForeground(Color.red);
		missing = true;
	    }

	}
	if (missing) {
	    Toolkit.getDefaultToolkit().beep();
	}
	return !(missing);
    }

    public String getInitials() {
	return inInitials.getText();
    }

    public String getYearOfBirth() {
	return inYoB.getText();
    }

    public String getCountryOfBirth() {
	return inConB.getText();
    }

    public String getCityOfBirth() {
	return inCityB.getText();
    }

    public String getResidence() {
	return inRes.getText();
    }

    public String getMTongue() {
	return inMTong.getText();
    }

    public short getGender() {
	return (short) cmBoxGender.getSelectedIndex();
    }

    public short getHandedness() {
	return (short) cmBoxHandedness.getSelectedIndex();
    }

}// class

